package com.service.Impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.utils.BaseService;
import com.dao.SysDictMapper;
import com.entity.SysDict;
import com.service.SysDictService;
import com.utils.DataTables;
import com.utils.RequestStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class SysDictServiceImpl extends BaseService implements SysDictService{
    @Autowired
    private SysDictMapper sysDictMapper;

    @Override
    public long getSysDictRowCount(){
        return sysDictMapper.getSysDictRowCount();
    }
    @Override
    public List<SysDict> selectSysDict(){
        return sysDictMapper.selectSysDict();
    }
    @Override
    public SysDict selectSysDictByObj(SysDict obj){
        return sysDictMapper.selectSysDictByObj(obj);
    }
    @Override
    public SysDict selectSysDictById(Integer id){
        return sysDictMapper.selectSysDictById(id);
    }
    @Override
    public int insertSysDict(SysDict value){
        return sysDictMapper.insertSysDict(value);
    }
    @Override
    public int insertNonEmptySysDict(SysDict value){
        return sysDictMapper.insertNonEmptySysDict(value);
    }
    @Override
    public int insertSysDictByBatch(List<SysDict> value){
        return sysDictMapper.insertSysDictByBatch(value);
    }
    @Override
    public int deleteSysDictById(Integer id){
        return sysDictMapper.deleteSysDictById(id);
    }
    @Override
    public int updateSysDictById(SysDict enti){
        return sysDictMapper.updateSysDictById(enti);
    }
    @Override
    public int updateNonEmptySysDictById(SysDict enti){
        return sysDictMapper.updateNonEmptySysDictById(enti);
    }

    @Override
    public DataTables getPageList(DataTables dataTables) {
        // 核心分页代码
        PageHelper.startPage((dataTables.getStart() / dataTables.getLength()) + 1, dataTables.getLength());
        PageHelper.orderBy("sort asc");

        //判断需要排序的类是否为空
        if(!StringUtils.isEmpty(dataTables.getColumn())){
            String column = propertyToColumn(SysDictMapper.class, dataTables.getColumn());
            if(!StringUtils.isEmpty(column)){
                PageHelper.orderBy(column + " " + dataTables.getOrder());
            }
        }
        PageInfo<SysDict> pageInfo = new PageInfo<SysDict>(sysDictMapper.getPageList(dataTables.getSearch(), dataTables.getSubSQL()));
        dataTables.setRecordsTotal(pageInfo.getTotal());
        dataTables.setRecordsFiltered(pageInfo.getTotal());
        dataTables.setData(pageInfo.getList() != null ? pageInfo.getList() : new ArrayList<SysDict>());

        return dataTables;
    }

    @Override
    public Map<String, Object> updateSysDict(SysDict sysDict) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (sysDictMapper.updateSysDictById(sysDict)>0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    @Override
    public Map<String, Object> addSysDict(SysDict sysDict) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (sysDictMapper.insertSysDict(sysDict)>0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    @Override
    public Map<String, Object> delScrap(List<Integer> idList) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (sysDictMapper.deleteByPrimaryKeys(idList)>0) {
            map.put("status", RequestStatus.SUCCESS);
        }else{
            map.put("status", RequestStatus.FAIL);
        }
        return map;
    }

    public SysDictMapper getSysDictMapper() {
        return this.sysDictMapper;
    }

    public void setSysDictMapper(SysDictMapper sysDictMapper) {
        this.sysDictMapper = sysDictMapper;
    }

}